package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.SParameterDetailsRemarks;
import com.ruoyi.system.domain.SProtectDeviceVersion;
import com.ruoyi.system.domain.vo.ParameterDetailsHistoryParamsVo;
import com.ruoyi.system.service.ISParameterDetailsRemarksService;
import com.ruoyi.system.service.ISProtectDeviceVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SParameterDetailsMapper;
import com.ruoyi.system.domain.SParameterDetails;
import com.ruoyi.system.service.ISParameterDetailsService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 参数详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Service
public class SParameterDetailsServiceImpl extends ServiceImpl<SParameterDetailsMapper, SParameterDetails> implements ISParameterDetailsService
{

    @Autowired
    private ISProtectDeviceVersionService protectDeviceVersionService;

    @Autowired
    private ISParameterDetailsRemarksService sParameterDetailsRemarksService;

    @Override
    public void checkChangeAndMark(List<List<SParameterDetails>> list) {
        // 只有一条记录不需要校验
        if(list.size() <= 1) return;

        List<SParameterDetails> temp = new ArrayList<>();
        List<SParameterDetails> sParameterDetails1 = list.get(0);
        List<SParameterDetails> sParameterDetails2 = list.get(1);
        List<SParameterDetails> sParameterDetails3;
        // 有三条记录的情况
        if(list.size() > 2){
            sParameterDetails3 = list.get(2);
            // 已最新的参数表数量为准
            for (int i = 0; i < sParameterDetails1.size(); i++) {
                SParameterDetails details1 = sParameterDetails1.get(i);
                SParameterDetails details2 = sParameterDetails2.get(i);
                SParameterDetails details3 = sParameterDetails3.get(i);
                // 所有记录都相等的情况
                if (details1.equals(details2) && details1.equals(details3)) {
                    // 将标记过的数据清空标记
                    if(details1.getDifferent() != 1){
                        details1.setDifferent(1);
                        temp.add(details1);
                    }
                    if(details2.getDifferent() != 1){
                        details2.setDifferent(1);
                        temp.add(details2);
                    }
                    if(details3.getDifferent() != 1){
                        details3.setDifferent(1);
                        temp.add(details3);
                    }

                }
                // 出现任意不相等的情况
                if (!details1.equals(details2) || !details1.equals(details3)) {
                    // 将未标记的数据打上标记
                    if(details1.getDifferent() != 2){
                        details1.setDifferent(2);
                        temp.add(details1);
                    }
                    if(details2.getDifferent() != 2){
                        details2.setDifferent(2);
                        temp.add(details2);
                    }
                    if(details3.getDifferent() != 2){
                        details3.setDifferent(2);
                        temp.add(details3);
                    }
                }
            }
        }else{
            // 只有两条记录的情况
            for (int i = 0; i < sParameterDetails1.size(); i++) {
                SParameterDetails details1 = sParameterDetails1.get(i);
                SParameterDetails details2 = sParameterDetails2.get(i);

                if (details1.equals(details2)) {
                    if(details1.getDifferent() != 1){
                        details1.setDifferent(1);
                        temp.add(details1);
                    }
                    if(details2.getDifferent() != 1){
                        details2.setDifferent(1);
                        temp.add(details2);
                    }
                }
                if (!details1.equals(details2)) {
                    if(details1.getDifferent() != 2){
                        details1.setDifferent(2);
                        temp.add(details1);
                    }
                    if(details2.getDifferent() != 2){
                        details2.setDifferent(2);
                        temp.add(details2);
                    }
                }
            }
        }
        // 有数据则更新
        if(temp.size() > 0){
            this.updateBatchById(temp);
        }

    }

    @Override
    public List<SParameterDetails> getHistory(ParameterDetailsHistoryParamsVo vo) {

        SProtectDeviceVersion byId = protectDeviceVersionService.getById(vo.getDeviceProtectVersionId());
        // 获取最近三个版本号
        SProtectDeviceVersion sProtectDeviceVersion = new SProtectDeviceVersion();
        sProtectDeviceVersion.setDeviceId(byId.getDeviceId());
        sProtectDeviceVersion.setProtectDeviceId(byId.getProtectDeviceId());
        QueryWrapper<SProtectDeviceVersion> queryWrapper = sProtectDeviceVersion.queryWrapper();
        queryWrapper.orderByDesc("file_date");
        queryWrapper.last("limit 0, 3");
        List<SProtectDeviceVersion> list = protectDeviceVersionService.list(queryWrapper);

        // 封装为map
        Map<String, SProtectDeviceVersion> versionMap = list.stream().collect(Collectors.toMap(SProtectDeviceVersion::getId, v -> v));
        // 获取id集合
        List<String> versionIds = list.stream().map(SProtectDeviceVersion::getId).collect(Collectors.toList());

        // 获取指定序号的最近三个历史版
        List<SParameterDetails> parameterDetailsList = this.list(new LambdaQueryWrapper<SParameterDetails>()
                .in(SParameterDetails::getDeviceProtectVersionId, versionIds)
                .eq(SParameterDetails::getSerialNo, vo.getSerialNo()));

        // 赋值文件字号和文件日期
        for (SParameterDetails sParameterDetails : parameterDetailsList) {
            String deviceProtectVersionId = sParameterDetails.getDeviceProtectVersionId();
            SProtectDeviceVersion deviceVersion = versionMap.get(deviceProtectVersionId);
            sParameterDetails.setFileNo(deviceVersion.getFileNo());
            sParameterDetails.setFileDate(deviceVersion.getFileDate());
        }
        return parameterDetailsList;
    }

    @Override
    public List<SParameterDetails> includeRemarksList(SParameterDetails sParameterDetails) {
        List<SParameterDetails> list = this.list(sParameterDetails.queryWrapper());
        for (SParameterDetails parameterDetails : list) {
            // 封装备注集合
            SParameterDetailsRemarks remarks = new SParameterDetailsRemarks();
            remarks.setParameterDetailsId(parameterDetails.getId());
            List<SParameterDetailsRemarks> remarksList = sParameterDetailsRemarksService.getList(remarks);
            parameterDetails.setRemarks(remarksList);
        }
        return list;
    }
}
